package cz.muni.fi.pa165.sportclubmanager.web;

import cz.muni.fi.pa165.sportclubmanager.dto.PlayerDTO;
import cz.muni.fi.pa165.sportclubmanager.service.PlayerService;
import java.util.Date;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 * ActionBean for PlayerService
 * 
 * @author mprokes
 */
@UrlBinding("/p/{$event}/{player.id}")
public class PlayersActionBean implements ActionBean {

    private static final String INDEX = "/players/index.jsp";
    private static final String EDIT = "/players/edit.jsp";
    
    private ActionBeanContext context;
    
    @SpringBean
    protected PlayerService playerService;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"save", "add"}, field = "firstName", required = true),
        @Validate(on = {"save", "add"}, field = "lastName", required = true),
        @Validate(on = {"save", "add"}, field = "birthDate", required = true),
        @Validate(on = {"save", "add"}, field = "height", required = true),
        @Validate(on = {"save", "add"}, field = "weight", required = true)
    })
    private PlayerDTO player;

    public PlayerDTO getPlayer() {
        return player;
    }

    public void setPlayer(PlayerDTO player) {
        this.player = player;
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadPlayerFromDatabase() {
        String ids = context.getRequest().getParameter("player.id");
        if (ids == null) {
            return;
        }
        player = playerService.find(Long.parseLong(ids));
    }
    
    @DefaultHandler
    public Resolution index() {
        return new ForwardResolution(INDEX);
    }

    public Resolution edit() {
        return new ForwardResolution(EDIT);
    }

    public Resolution add() {
        try{
          playerService.create(player);        
          getContext().getMessages().add(new SimpleMessage("Player added"));                                                                      
        }
        catch(Exception ex){
           getContext().getMessages().add(new SimpleMessage("error: "+ex.getLocalizedMessage()));                                                                      
        }            
        return new RedirectResolution(this.getClass(), "index");
    }

    public Resolution delete() {        
        try{
          playerService.remove(player);        
          getContext().getMessages().add(new SimpleMessage("Player removed"));                                                                      
        }
        catch(Exception ex){
           getContext().getMessages().add(new SimpleMessage("error: "+ex.getLocalizedMessage()));                                                                      
        }                    
        return new RedirectResolution(this.getClass(), "index");
    }

    public Resolution save() {
        try{
          playerService.update(player);        
          getContext().getMessages().add(new SimpleMessage("Player updated"));                                                                      
        }
        catch(Exception ex){
           getContext().getMessages().add(new SimpleMessage("error: "+ex.getLocalizedMessage()));                                                                      
        }                    
        return new RedirectResolution(this.getClass(), "index");
    }
    
    public List<PlayerDTO> getPlayers() {
        return playerService.findAll();
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}